﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Json;
using WLFramework.Web;

namespace SampleSite.admin.ajax.table
{
    /// <summary>
    /// columns: [[
    //    { field: 'id', hidden: true },
    //    { title: 'KEY', field: 'key', width: 60 },
    //    { title: '名称', field: 'name', width: 60 },
    //    { title: '类型', field: 'type', width: 60 },
    //    { title: '必填', field: 'required', width: 60 },
    //    { title: '控件', field: 'control', width: 60 },
    //    { title: '验证规则', field: 'regular', width: 60 },
    //    { title: '小数位数', field: 'precision', width: 60 },
    //    { title: '最小长度', field: 'min_length', width: 60 },
    //    { title: '最大长度', field: 'max_length', width: 60 },
    //    { title: '最小日期', field: 'min_date', width: 100 },
    //    { title: '最大日期', field: 'max_date', width: 100 }
    //]],
    /// </summary>
    public class columns : AjaxHttpHandler
    {
        public override void OnProcessRequest(HttpContext context)
        {
            int id = context.GetValueByName<int>("id");
            if (id <= 0)
                throw new ArgumentException("参数错误");

            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                var query = from p in db.Context.dy_DynamicColumns
                            where p.TableId == id
                            select new
                            {
                                id = p.Id,
                                key = p.ColumnName,
                                name = p.DisplayName,
                                type = p.TypeId,
                                required = p.Required,
                                control = p.ControlId,
                                regular = p.RegularId,
                                precision = p.Precision,
                                min_length = p.MinLength,
                                max_length = p.MaxLength,
                            };

                context.Response.Write(JSON.Instance.ToJSON(query.ToList()));
            }
        }
    }
}